﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DBProvider;
using DBProvider.Providers;

namespace HealthCommunity.Controls
{
	public partial class UCDrugStoreList : System.Web.UI.UserControl
	{
		private IDrugStoreProvider _DrugStoreProvider;

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				BindData();
			}
		}

		protected void gvDrugStore_RowCommand(object sender, GridViewCommandEventArgs e)
		{
			if (e.CommandName.Equals("remove"))
			{
				if (e.CommandArgument != null)
				{
					var drugStoreID = int.Parse(e.CommandArgument.ToString());

					if (_DrugStoreProvider == null)
					{
						_DrugStoreProvider = new DrugStoreProvider();
					}

					if (_DrugStoreProvider.DeleteDrugStore(drugStoreID))
					{
						BindData();
						ltrMsg.Text = "删除成功";
					}
					else
					{
						ltrMsg.Text = "删除时遇到错误";
					}
				}
			}

			if (e.CommandName.Equals("edit"))
			{
				if (e.CommandArgument != null)
				{
					var drugStoreID = int.Parse(e.CommandArgument.ToString());

					Response.Redirect("~/DrugStoreEdit.aspx?id=" + drugStoreID.ToString());
				}
			}
		}

		private void BindData()
		{
			var user = SiteContext.Current.CurrentUser;

			if (user != null && user.Role != null && user.Role.RoleName.ToLower().Equals("admin"))
			{
				_DrugStoreProvider = new DrugStoreProvider();

				gvDrugStore.DataSource = _DrugStoreProvider.GetAllDrugStore();
				gvDrugStore.DataBind();
			}
		}
	}
}